﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WL_Store.BLL;
using WL_Store.Model;

namespace WL_Store.Web.subpage
{
    public partial class Refund_edit : System.Web.UI.Page
    {
        protected RefundBLL bll = new RefundBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            BindGoodsType();

                string orderId = Request.QueryString["orderID"];

                RefundModel model = bll.GetRefund(orderId);
                if (model!=null)
                {
                    txt_edit_orderID.Text = orderId;
                    txt_edit_commodityId.Text = model.commodityId.ToString();
                    txt_edit_comName.Text = model.comName.ToString();
                    ddl_edit_categoryId.SelectedValue = model.categoryId.ToString();
                    txt_edit_transactionAmount.Text = model.transactionAmount.ToString();
                    txt_edit_tradingHour.Value = model.tradingHour.ToString();
                    txt_edit_refundMoney.Text = model.refundMoney.ToString();
                    txt_edit_refundAmount.Text = model.refundAmount.ToString();
                    rbt_edit_state.SelectedValue = model.state?"1":"0";
                    txt_edit_explain.Text = model.explain;
                }
                

            }
        }

        protected void btn_edit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {

           
            //获取用户更更改的值
            string commodityId = txt_edit_commodityId.Text.Trim();
            string comName = txt_edit_comName.Text.Trim();
            int categoryId = Convert.ToInt32(ddl_edit_categoryId.SelectedValue);
            string transactionAmount = txt_edit_transactionAmount.Text.Trim();
            string tradingHour = txt_edit_tradingHour.Value.Trim();
            string refundMoney = txt_edit_refundMoney.Text.Trim();
            string refundAmount = txt_edit_refundAmount.Text.Trim();
            int state = Convert.ToInt32(rbt_edit_state.SelectedValue);
            string explain = txt_edit_explain.Text.Trim();

            string orderId = Request.QueryString["orderID"];
                RefundModel model = new RefundModel {
                    orderID = orderId,
                    commodityId = Convert.ToInt32(commodityId),
                    comName = comName,
                    categoryId = categoryId,
                    transactionAmount = Convert.ToDouble(transactionAmount),
                    tradingHour = Convert.ToDateTime(tradingHour),
                    refundMoney = Convert.ToDouble(refundMoney),
                    refundAmount = Convert.ToDouble(refundAmount),
                    state = Convert.ToBoolean(state),
                    explain = explain,
                
                };
                if (bll.UpdateRefund(model))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('修改成功！');location.href='Refund.aspx'</script>");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('修改失败！')</script>");
                }

            }
        }

        /// <summary>
        /// 绑定商品类别数据源
        /// </summary>
        private void BindGoodsType()
        {
            //读取商品类别信息
            DataTable dt = bll.GetGoodsType();
            //数据源赋值
            ddl_edit_categoryId.DataSource = dt;
            //显示字段
            ddl_edit_categoryId.DataTextField = "category";
           
            //值字段
            ddl_edit_categoryId.DataValueField = "categoryId";
            
            //绑定
            ddl_edit_categoryId.DataBind();
            
           
        }
    }
}